package com.cictec.middleware.tde.rm.protocol.rm.encode;

import java.nio.ByteBuffer;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.tde.exception.MessageEncoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageEncoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageEncoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x29TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 0x29 切换线路指令应答
 * @author Administrator
 *
 */
@TerminalMessageEncoder(0x29)
public class Rm0x29Encoder extends AbstractMessageEncoder {

	@Override
	public byte[] encode(TerminalMessage message) throws MessageEncoderException {
		
		Rm0x29TerminalMessage msg = (Rm0x29TerminalMessage) message;

		ByteBuffer out = ByteBuffer.allocate(256);
		out.put(RmBinaryUtils.dateToDwordRM(msg.getCreateTime()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getManufactureNumber()));
		out.put(BinaryUtils.str2Bcd(msg.getTerminalName()));
		
		byte [] ss = new byte[10];
		byte [] bnumber = BinaryUtils.stringToBytes(msg.getSerialNumber());	// 终端出厂序列号 	HEX 10 字节
		if(bnumber.length < 10){
			for (int i = 0; i < bnumber.length; i++) {
				ss[i] = bnumber[i];
			}
		}
		out.put(ss);
		
		out.put(BinaryUtils.intToWord(msg.getOriginalLineNumber()));
		out.put(BinaryUtils.intToWord(msg.getDestinationNumber()));
		out.put(BinaryUtils.intToUnsignedByte(msg.getChangeStatus()));
		out.put(BinaryUtils.intToWord(msg.getReserved()));

		return toBytes(out);
	}

}
